package zzs.generalframework.console.core.authorization.authrolefunctionmap.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import zzs.generalframework.common.core.authorization.authrolefunctionmap.AuthRoleFunctionMapBO;
import zzs.generalframework.common.core.authorization.authrolefunctionmap.AuthRoleFunctionMapInfo;
import zzs.generalframework.common.core.authorization.authrolefunctionmap.AuthRoleFunctionMapPO;
import zzs.generalframework.console.common.exception.ConsoleException;
import zzs.generalframework.console.common.exception.ConsoleRollbackException;
import zzs.generalframework.console.common.model.ConsoleReturnBO;
import zzs.generalframework.console.common.model.ConsoleReturnBO.ConsoleResult;
import zzs.generalframework.console.core.authorization.authrolefunctionmap.dao.IAuthRoleFunctionMapDao;
import zzs.generalframework.console.core.authorization.authrolefunctionmap.service.IAuthRoleFunctionMapService;
import zzs.generalframework.util.core.reflect.BeanUtils;

/**
 * @title
 * @description
 * @project zzs-console
 * @package zzs.generalframework.console.core.authorization.authrolefunctionmap.service.impl
 * @author zzs
 * @date 2018年2月2日
 * @version 1.0
 */
@Service("zzs.generalframework.console.core.authorization.authrolefunctionmap.service.impl.authRoleFunctionMapServiceImpl")
public class AuthRoleFunctionMapServiceImpl implements IAuthRoleFunctionMapService
{

	private Logger logger = LoggerFactory.getLogger(AuthRoleFunctionMapServiceImpl.class);
	@Autowired
	BeanUtils beanUtils;
	@Autowired
	IAuthRoleFunctionMapDao authRoleFunctionMapDao;

	@Override
	public ConsoleReturnBO saveAuthRoleFunctionMap(AuthRoleFunctionMapBO authRoleFunctionMapBO)
	{
		logger.debug("====保存权限角色功能映射信息开始====");
		logger.debug("====保存权限角色功能映射信息.入参:AuthRoleFunctionMapBO=[{}]====", authRoleFunctionMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRoleFunctionMapPO authRoleFunctionMapPO = new AuthRoleFunctionMapPO();
		copyAuthRoleFunctionMapBO2AuthRoleFunctionMapPO(authRoleFunctionMapBO, authRoleFunctionMapPO);
		Boolean insertResult = authRoleFunctionMapDao.insert(authRoleFunctionMapPO);
		if (insertResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====保存权限角色功能映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限角色功能映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO saveAuthRoleFunctionMap(List<AuthRoleFunctionMapBO> authRoleFunctionMapBOList) throws ConsoleException
	{
		logger.info("====保存权限角色功能映射信息列表开始====");
		logger.debug("====保存权限角色功能映射信息列表.入参:AuthRoleFunctionMapBOList=[{}]====", authRoleFunctionMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleFunctionMapBO itemBO : authRoleFunctionMapBOList)
		{
			ConsoleReturnBO itemResult = saveAuthRoleFunctionMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====保存权限角色功能映射信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====保存权限角色功能映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO getAuthRoleFunctionMap(AuthRoleFunctionMapInfo authRoleFunctionMapInfo)
	{
		logger.info("====获取权限角色功能映射信息开始====");
		logger.debug("====获取权限角色功能映射信息.入参:AuthRoleFunctionMapInfo=[{}]====", authRoleFunctionMapInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthRoleFunctionMapPO> authRoleFunctionMapPOList = authRoleFunctionMapDao.select(authRoleFunctionMapInfo);
		if (authRoleFunctionMapPOList == null || authRoleFunctionMapPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else if (authRoleFunctionMapPOList.size() == 1)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			AuthRoleFunctionMapBO authRoleFunctionMapBO = new AuthRoleFunctionMapBO();
			copyAuthRoleFunctionMapPO2AuthRoleFunctionMapBO(authRoleFunctionMapPOList.get(0), authRoleFunctionMapBO);
			returnConsoleReturnBO.setReturnObj(authRoleFunctionMapBO);
		}
		else
		{
			returnConsoleReturnBO.setMessage("记录不唯一");
			AuthRoleFunctionMapBO authRoleFunctionMapBO = new AuthRoleFunctionMapBO();
			copyAuthRoleFunctionMapPO2AuthRoleFunctionMapBO(authRoleFunctionMapPOList.get(0), authRoleFunctionMapBO);
			returnConsoleReturnBO.setReturnObj(authRoleFunctionMapBO);
		}
		logger.debug("====获取权限角色功能映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限角色功能映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO listAuthRoleFunctionMap(AuthRoleFunctionMapInfo authRoleFunctionMapInfo)
	{
		logger.info("====获取权限角色功能映射信息列表开始====");
		logger.debug("====获取权限角色功能映射信息列表.入参:AuthRoleFunctionMapInfo=[{}]====", authRoleFunctionMapInfo);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		List<AuthRoleFunctionMapPO> authRoleFunctionMapPOList = authRoleFunctionMapDao.select(authRoleFunctionMapInfo);
		if (authRoleFunctionMapPOList == null || authRoleFunctionMapPOList.size() == 0)
		{
			returnConsoleReturnBO.setMessage("未获取到记录");
		}
		else
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
			List<AuthRoleFunctionMapBO> resultList = new ArrayList<AuthRoleFunctionMapBO>();
			for (AuthRoleFunctionMapPO itemPO : authRoleFunctionMapPOList)
			{
				AuthRoleFunctionMapBO itemBO = new AuthRoleFunctionMapBO();
				copyAuthRoleFunctionMapPO2AuthRoleFunctionMapBO(itemPO, itemBO);
				resultList.add(itemBO);
			}
			returnConsoleReturnBO.setReturnObj(resultList);
		}
		logger.debug("====获取权限角色功能映射信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====获取权限角色功能映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthRoleFunctionMap(AuthRoleFunctionMapBO authRoleFunctionMapBO)
	{
		logger.info("====更新权限角色功能映射信息开始====");
		logger.debug("====更新权限角色功能映射信息.入参:AuthRoleFunctionMapBO=[{}]====", authRoleFunctionMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRoleFunctionMapPO authRoleFunctionMapPO = new AuthRoleFunctionMapPO();
		copyAuthRoleFunctionMapBO2AuthRoleFunctionMapPO(authRoleFunctionMapBO, authRoleFunctionMapPO);
		Boolean updateResult = authRoleFunctionMapDao.update(authRoleFunctionMapPO);
		if (updateResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====更新权限角色功能映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限角色功能映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO updateAuthRoleFunctionMap(List<AuthRoleFunctionMapBO> authRoleFunctionMapBOList) throws ConsoleException
	{
		logger.info("====更新权限角色功能映射信息列表开始====");
		logger.debug("====更新权限角色功能映射信息列表.入参:List<AuthRoleFunctionMapBO>=[{}]====", authRoleFunctionMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleFunctionMapBO itemBO : authRoleFunctionMapBOList)
		{
			ConsoleReturnBO itemResult = updateAuthRoleFunctionMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====更新权限角色功能映射信息列表.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====更新权限角色功能映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthRoleFunctionMap(AuthRoleFunctionMapBO authRoleFunctionMapBO)
	{
		logger.info("====删除权限角色功能映射信息开始====");
		logger.debug("====删除权限角色功能映射信息.入参:AuthRoleFunctionMapBO＝[{}]======", authRoleFunctionMapBO);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		AuthRoleFunctionMapPO authRoleFunctionMapPO = new AuthRoleFunctionMapPO();
		copyAuthRoleFunctionMapBO2AuthRoleFunctionMapPO(authRoleFunctionMapBO, authRoleFunctionMapPO);
		Boolean deleteResult = authRoleFunctionMapDao.delete(authRoleFunctionMapPO);
		if (deleteResult)
		{
			returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		}
		logger.debug("====删除权限角色功能映射信息.出参:ConsoleReturnBO=[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限角色功能映射信息结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public ConsoleReturnBO removeAuthRoleFunctionMap(List<AuthRoleFunctionMapBO> authRoleFunctionMapBOList) throws ConsoleException
	{
		logger.info("====删除权限角色功能映射信息列表开始====");
		logger.debug("====删除权限角色功能映射信息列表.入参:List<AuthRoleFunctionMapBO>＝[{}]======", authRoleFunctionMapBOList);
		ConsoleReturnBO returnConsoleReturnBO = new ConsoleReturnBO();
		returnConsoleReturnBO.setResult(ConsoleResult.SUCCESS);
		for (AuthRoleFunctionMapBO itemBO : authRoleFunctionMapBOList)
		{
			ConsoleReturnBO itemResult = removeAuthRoleFunctionMap(itemBO);
			if (ConsoleResult.FAILD.equals(itemResult.getResult()))
			{
				returnConsoleReturnBO.setResult(ConsoleResult.FAILD);
				returnConsoleReturnBO.setMessage(itemResult.getMessage());
				throw new ConsoleRollbackException();
			}
		}
		logger.debug("====删除权限角色功能映射信息列表.出参:ConsoleReturnBO＝[{}]====", returnConsoleReturnBO);
		logger.info("====删除权限角色功能映射信息列表结束====");
		return returnConsoleReturnBO;
	}

	@Override
	public Long countAuthRoleFunctionMap(AuthRoleFunctionMapBO authRoleFunctionMapBO)
	{
		logger.info("====统计权限角色功能映射信息开始====");
		logger.debug("====统计权限角色功能映射信息。入参：AuthRoleFunctionMapBO＝[{}]======", authRoleFunctionMapBO);
		Long returnLong = new Long(0);
		AuthRoleFunctionMapPO authRoleFunctionMapPO = new AuthRoleFunctionMapPO();
		copyAuthRoleFunctionMapBO2AuthRoleFunctionMapPO(authRoleFunctionMapBO, authRoleFunctionMapPO);
		returnLong = authRoleFunctionMapDao.count(authRoleFunctionMapPO);
		logger.debug("====统计权限角色功能映射信息.出参:Long＝[{}]======", returnLong);
		logger.info("====统计权限角色功能映射信息结束====");
		return returnLong;
	}

	private void copyAuthRoleFunctionMapBO2AuthRoleFunctionMapPO(AuthRoleFunctionMapBO authRoleFunctionMapBO, AuthRoleFunctionMapPO authRoleFunctionMapPO)
	{
		beanUtils.copyBeanNotNull(authRoleFunctionMapBO, authRoleFunctionMapPO);
		if (authRoleFunctionMapBO.getSysID() != null)
		{
			authRoleFunctionMapPO.setSeqID(authRoleFunctionMapBO.getSysID());
		}
	}

	private void copyAuthRoleFunctionMapPO2AuthRoleFunctionMapBO(AuthRoleFunctionMapPO authRoleFunctionMapPO, AuthRoleFunctionMapBO authRoleFunctionMapBO)
	{
		beanUtils.copyBeanNotNull(authRoleFunctionMapPO, authRoleFunctionMapBO);
		if (authRoleFunctionMapPO.getSeqID() != null)
		{
			authRoleFunctionMapBO.setSysID(authRoleFunctionMapPO.getSeqID());
		}
	}

}
